/**  
* @Title: IKAnaylzer.java
* @Package com.lemon.conf.lucene
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年6月2日
* @version V1.0  
*/
package com.lemon.conf.lucene;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.dic.Dictionary;
import org.wltea.analyzer.dic.Hit;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年6月2日
 * @版本 V 1.0 
 */
public class IKAnaylzerUtil {

	public static String str = "白粉红色女款小孩子处理舞鞋体育黑色广场秋季瑜伽女式舞蹈鞋";

	public static void main( String args[] ) throws IOException {
		Configuration cfg = DefaultConfig.getInstance();
		cfg.setUseSmart(true);
		Dictionary.initial(cfg);
		Dictionary dict = Dictionary.getSingleton();
		List<String> words = new ArrayList<String>();
		words.add("志向(心理学)");
		words.add("芳香-L-氨基酸脱羧酶类");
		words.add("atp柠檬酸（pro-S）裂合酶");
		dict.addWords(words);//添加分词集合

		//停用词集合 , 将制定词汇不显现,进行二次分隔
		List<String> words2 = new ArrayList<String>();
		//		words2.add("志向");
		//		words2.add("心理学");
		dict.disableWords(words2);//停用词集合

		String ss = "";
		List<String> list = queryWords(str);
		for(int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
			ss += " title like '%" + list.get(i) + "%' or ";
		}
		System.out.println(ss);

	}

	/**
	 * IK 分词
	 *
	 * @param query
	 * @return
	 * @throws IOException
	 */
	public static List<String> queryWords( String query ) throws IOException {
		List<String> list = new ArrayList<String>();
		StringReader input = new StringReader(query.trim());
		IKSegmenter ikSeg = new IKSegmenter(input, true);// true用智能分词，false细粒度
		for(Lexeme lexeme = ikSeg.next(); lexeme != null; lexeme = ikSeg.next()) {
			list.add(lexeme.getLexemeText());
		}
		return list;
	}

}
